package com.mathijsjh.pwdhash.pwdhash;


import java.security.GeneralSecurityException;

import com.mathijsjh.pwdhash.sharp2java.ArgumentException;
import com.mathijsjh.pwdhash.sharp2java.ArgumentNullException;


public class PwdHash {
	
	public static String GenerateHashedPassword(String siteAddress, String sitePassword) throws ArgumentNullException, ArgumentException {
		
		if (siteAddress == null)
			throw new ArgumentNullException("siteAddress",
			                                "Please specify a valid domain");
		if (siteAddress == "")
			throw new ArgumentException("Please specify a valid domain",
			                            "siteAddress");
		if (sitePassword == null)
			throw new ArgumentNullException("sitePassword",
			                                "Please specify a valid password");
		if (sitePassword == "")
			throw new ArgumentException("Please specify a valid password",
			                            "sitePassword");
		
		if (sitePassword.length() <= 4)
			throw new ArgumentException("Your password must contain more than 4 characters.",
										"sitePassword");
		
		String domain = DomainExtractor.ExtractDomain(siteAddress);
		
		if (domain == "")
			throw new ArgumentException("Please specify a valid domain",
			                            "siteAddress");
		
		PasswordHasher hasher = new PasswordHasher(domain, sitePassword);
		
		String hashedPassword;
		try {
			hashedPassword = hasher.GetHashedPassword();
		} catch (GeneralSecurityException e) {
			throw new Error("Generation of Passwordhash failed", e);
		}
		
		return hashedPassword;
	}
	
}
